mini.parse();
var baseUrl = "http://localhost:8080/DSMP";
var grid = mini.get("grid1");
var iframe = document.getElementById("iframeId");
var tree = mini.get("tree1");//表示获取到树这个对象
var treeData;

//分页控件
var pager = grid.getBottomPager();
var form = new mini.Form("#form1");
var qqq = [
    {"id": "0", "text": "小型车"},
    {"id": "1", "text": "中型车"},
    {"id": "2", "text": "大型车"},
]
var www = [
    {"id": "0", "text": "空闲"},
    {"id": "1", "text": "有计划"},
    {"id": "2", "text": "忙碌"},
]
var eee = [
    {"id": "0-20", "text": "0-20"},
    {"id": "20-50", "text": "20-50"},
    {"id": "50-100", "text": "50-100"},
    {"id": "100-150", "text": "100-150"},
]
var griddata = [
    {"id": "rs", "carInfoId": "1", "carName": "王多多", "gender": 1, "carPlateNumber": 28, "carHealth": "usa"},
    {"id": "cw", "carInfoId": "2", "carName": "王健林", "gender": 1, "carPlateNumber": 19, "carHealth": "cn"},
    {"dept_id": "cw", "carInfoId": "3", "carName": "王思聪", "gender": 1, "carPlateNumber": 21, "carHealth": "jp"},
    {"dept_id": "rs", "carInfoId": "4", "carName": "胡歌", "gender": 1, "carPlateNumber": 21, "carHealth": "en"},
    {"dept_id": "rs", "carInfoId": "5", "carName": "张卫健", "gender": 2, "carPlateNumber": 21, "carHealth": "de"},
    {"dept_id": "rs", "carInfoId": "6", "carName": "血杀保健", "gender": 1, "carPlateNumber": 21, "carHealth": "fr"}
]
grid.setData(griddata);
var carType = mini.get("carType");
carType.setData(qqq);
var carState = mini.getByName("carState");
carState.setData(www);
// var salary = mini.get("salary");
// salary.setData(eee);
//页面初始化
(function () {
    request('0', pager.getPageSize());
    folderTree();
})();


//跳转增加页面
function addRow() {
    mini.open({

        targetWindow: window,
        url: "uploadfiles.html",
        title: "文档上传",
        height: "90%",
        width: "90%",
        showMaxButton: true,
        showModal: Boolean,
        allowResize: Boolean,
        setReadOnly: false,
        onload: function () {
            var iframe = this.getIFrameEl();//备份这个跨子页面窗口
            var data = {action: "new"};//父传值给子页面
        },
        ondestroy: function (action) {
            if (action == "ok") {
                // request('0',pager.getPageSize());
                grid.reload();    //页面重新加载
            }
        }
    })
}

//跳转编辑页面
function editRow() {
    var row = grid.getSelected();

    if (row != undefined) {
        mini.open({

            targetWindow: window,
            url: "carinfodemo.html",
            title: "新增会议室",
            height: "90%",
            width: "90%",
            showMaxButton: true,
            showModal: Boolean,
            allowResize: Boolean,
            setReadOnly: false,
            onload: function () {
                var iframe = this.getIFrameEl();//备份这个跨子页面窗口
                var data = {action: "edit", row: row};//父传值给子页面
                iframe.contentWindow.SetData(data);//跨字串后页面渲染data
            },
            ondestroy: function (action) {
                if (action == "edit") {
                    // request('0',pager.getPageSize());
                    grid.reload();    //页面重新加载

                }
            }
        })
    } else {
        mini.alert("请选择一条数据")
    }

}

// 删除
function removeRow() {
    var rows = grid.getSelecteds();
    console.log(rows);
    var carIds = []
    rows.forEach(function (item) {
        carIds.push(item.carInfoId)
    })
    console.log(carIds);
    carIds = JSON.stringify(carIds);
    if (rows.length > 0) {        //当一条都不选时提示让选一条
        console.log(carIds);
        var carId = grid.getSelected().carInfoId;
        $.ajax({
            url: baseUrl + "/carInfo/delCar.do",
            data: carIds,
            type: "post",
            success: function (data) {
                mini.alert(data.msg)
                // request();
                grid.reload();
            }
        })
    } else {
        mini.alert("请至少选中一行记录")
    }
}

var arr;

//上传
function upload() {
    mini.open({
        targetWindow: window,
        url: "uploadfiles.html",
        title: "文档上传",
        height: "90%",
        width: "90%",
        showMaxButton: true,
        showModal: Boolean,
        allowResize: Boolean,
        setReadOnly: false,
        onload: function () {
            var iframe = this.getIFrameEl();//备份这个跨子页面窗口
            var data = {folderTree: treeData};//父传值给子页面
            iframe.contentWindow.SetData(data);//跨字串后页面渲染data
        },
        ondestroy: function (action) {
            if (action == "edit") {
                // request('0',pager.getPageSize());
                grid.reload();    //页面重新加载

            }
        }
    })

}

//下载
function download() {
    alert("下载按钮js");//这行不用请删掉
    var row = grid.getSelected();
    console.log(row);
    // // row = JSON.parse(row);         //将这一行值转化成字符串
    //     //     $.ajax({
    //     //     url:"http://localhost:8080/DSMP/download?filePath="+row.docFileRemotePath,                //放接口
    //     //     type: "post",
    //     //     contentType: "application/json",
    //     //     data:row,
    //     //     success: function(data) {//请求成功完成后要执行的方法
    //     //         console.log(data);
    //     //         // grid.setData(data.data);
    //     //         // pager.set({"totalCount": data.total})
    //     //     },
    //     //     error:function(data){
    //     //         mini.alert(data);
    //     //     }
    //     // });
    var filePath = row.docFileRemotePath
    console.log(filePath)
    window.open("http://localhost:8080/DSMP/download?filePath=" + filePath, "下载", filePath)
}

function request(pageIndex, pageSize) {
    var pageIndex = pager.getPageIndex()
    var pageSize = pager.getPageSize();

    var pageObj = {
        current: pageIndex,
        size: pageSize,
    }

    var pageStr = JSON.stringify(pageObj);
    $.ajax({
        url: baseUrl + "/docFile/getAllFiles.do",
        type: "post",
        contentType: "application/json",
        data: pageStr,
        success: function (data) {//请求成功完成后要执行的方法
            console.log(data);
            // data=JSON.parse(data)    //如果字符串的话，转换成json
            grid.setData(data.obj);
            pager.set({"totalCount": data.attributes.total})
        },
        error: function (action) {
            mini.alert(action);
        }
    });
}

//根据筛选条件查询
function find() {
    var aaa = form.getData();     //获取form上组件的参数

    console.log(aaa);
    // k1,k2是后端自己定的字段，后面打点的字段是前面组件name的字段
    var data = {
        k1: aaa.carName,
        k2: aaa.carType,
        k3: aaa.carState,
        k4: aaa.salary
    }
    console.log(data);
    data = JSON.stringify(data);
    console.log(data);
    $.ajax({
        url: baseUrl + "/carInfo/queryWhereCoulmn.do",
        type: "post",
        contentType: "application/json",
        data: aaa,
        success: function (data) {//请求成功完成后要执行的方法
            console.log(data);

            grid.setData('');
            grid.setData(data.obj);
            mini.after(data.msg)
        },
        error: function (data) {
            mini.alert(data.msg);
        }
    });
}


//分页
grid.on("beforeload", function (e) {
    //阻止本来的翻页功能
    e.cancel = true;
    //当前页码
    var cunrrentPageIndex = grid.getPageIndex();
    //点击按钮之后的页码
    var pageIndex = e.data.pageIndex;
    //设置当前页数值
    pager.set({"pageIndex": pageIndex});
    //数据总数
    //var pageCount = pubPageCount;
    pager.set({"pageIndex": pageIndex});
    request(pageIndex + 1, pager.getPageSize());
});

//刷新
function onRefresh(e) {
    list(pager.getPageIndex(), pager.getPageSize());
}


//条件查询清空条件
function clean() {
    form.reset();
}

//刷新按钮
function refresh() {
    form.reset();
    request('0', pager.getPageSize());
}

// 打印
function print() {
    //打印
    console.log("print");

}


//文件预览
function preview() {
    var row = grid.getSelected();
    console.log(row)
    if (row) {
        var id = row.docFileId;
        window.open("http://localhost:8080/DSMP/pdfjs/web/viewer.html?file=" + "http://localhost:8080/DSMP/docFile/preview/" + id);
    } else {
        mini.alert("请选择一条记录")
    }
}


//获取文件夹树形结构
function folderTree() {
    $.ajax({
        url: baseUrl + "/docFolder/getFolderTree.do",
        type: "post",
        success: function (data) {
            console.log(data);
            console.log(data.obj, "树结构")
            tree.loadData(data.obj)
            treeData = data.obj
        }
    })

}